package com.aizhixin.lab.docs.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.template.dto.TemplateDTO;
import com.aizhixin.lab.docs.dto.DocContentDTO;
import com.aizhixin.lab.docs.entity.DocContent;
import com.aizhixin.lab.docs.entity.DocMenu;
import com.aizhixin.lab.docs.service.HelpDocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/docs/help")
@Api(value = "帮助文档相关API")
public class HelpDocController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private HelpDocService helpDocService;

    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询帮助文档目录列表", response = Void.class, notes = "查询帮助文档目录列表<br>@author hsh")
    public ResponseEntity<?> getMenuList(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "type 1:管理员 2:教师 3:学生", required = false) @RequestParam(value = "type", required = false) Integer type) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        List<DocMenu> result = helpDocService.getMenuList(type);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加帮助文档目录", response = Void.class, notes = "添加帮助文档目录<br>@author hsh")
    public ResponseEntity<?> saveMenu(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "id", required = false) @RequestParam(value = "id", required = false) String id,
                                      @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                      @ApiParam(value = "parentId", required = false) @RequestParam(value = "parentId", required = false) String parentId,
                                      @ApiParam(value = "type 1:管理员 2:教师 3:学生", required = false) @RequestParam(value = "type", required = false) Integer type,
                                      @ApiParam(value = "menuNo", required = false) @RequestParam(value = "menuNo", required = false) String menuNo) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        Map<String, Object> result = helpDocService.saveMenu(id, name, parentId, type, menuNo);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveDoc", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加帮助文档", response = Void.class, notes = "添加帮助文档<br>@author hsh")
    public ResponseEntity<?> saveDoc(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "type 10:理论课 20:实验课", required = false) @RequestBody DocContentDTO dto) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        Map<String, Object> result = helpDocService.saveDoc(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程模板", response = Void.class, notes = "删除课程模板<br>@author hsh")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "menuId", required = true) @RequestParam(value = "menuId") String menuId) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        Map<String, Object> result = helpDocService.delete(menuId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getDoc", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询帮助文档", response = Void.class, notes = "查询帮助文档<br>@author hsh")
    public ResponseEntity<?> getDoc(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "menuId", required = true) @RequestParam(value = "menuId") String menuId) {
//        AccountDTO account = accountService.getUserByToken(token);
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
        DocContent result = helpDocService.getDoc(menuId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
